﻿<Control x:Class="MiniUML.Plugins.UmlClassDiagram.Resources.Shapes.GenericUmlContainerShape"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
         xmlns:cm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
         xmlns:aero="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
         xmlns:viewutils="clr-namespace:MiniUML.View.Utilities;assembly=MiniUML.View"
         xmlns:utilities="clr-namespace:MiniUML.Plugins.UmlClassDiagram.Utilities"
         xmlns:controls="clr-namespace:MiniUML.View.Controls;assembly=MiniUML.View"
         xmlns:shapes="clr-namespace:MiniUML.Plugins.UmlClassDiagram.Resources.Shapes"
         MinWidth="130">

    <Control.Resources>

        <viewutils:ShapeTemplateSelector x:Key="ShapeTemplateSelector" />
        
        <utilities:GroupByTypeConverter x:Key="GroupByTypeConverter"/>

        <CollectionViewSource x:Key="ItemsSource" Source="{Binding Path=Elements}">
            <CollectionViewSource.GroupDescriptions>
                <d:PropertyGroupDescription PropertyName="Name.LocalName" Converter="{StaticResource GroupByTypeConverter}"/>
            </CollectionViewSource.GroupDescriptions>
            <CollectionViewSource.SortDescriptions>
                <cm:SortDescription PropertyName="Name.LocalName"/>
                <cm:SortDescription PropertyName="Attribute[Name].Value"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

    </Control.Resources>

    <Control.Template>

        <ControlTemplate TargetType="{x:Type shapes:GenericUmlContainerShape}">

            <!--aero:SystemDropShadowChrome -->
                
            <Border Background="{TemplateBinding Property=Background}"
                    BorderBrush="{TemplateBinding Property=BorderBrush}"
                    BorderThickness="{TemplateBinding Property=BorderThickness}"
                    CornerRadius="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type shapes:GenericUmlContainerShape}}, Path=CornerRadius}">

                <StackPanel>

                    <StackPanel Margin="8,5,8,16">

                        <controls:SingleClickBlock>
                            <TextBox Text="{Binding Path=Attribute[Name].Value}" FontWeight="Bold" Foreground="Black"
                                     IsTabStop="False" BorderThickness="0" Background="Transparent"
                                     Style="{DynamicResource UmlContainerNameStyle}"/>
                        </controls:SingleClickBlock>

                        <controls:SingleClickBlock>
                            <TextBox Text="{Binding Path=Attribute[Description].Value}" FontSize="11" Foreground="Black"
                                     IsTabStop="False" BorderThickness="0" Background="Transparent"/>
                        </controls:SingleClickBlock>

                        <!--
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="→" FontFamily="Arial" Margin="3,0,5,0"/>
                            <TextBlock Text="{Binding Path=Attribute[BaseClass].Value}" FontSize="11" Foreground="Black"/>
                        </StackPanel>
                        -->

                    </StackPanel>

                    <ListBox x:Name="_listBox"
                             Margin="0,0,0,8"
                             SnapsToDevicePixels="True"
                             BorderThickness ="0,1,0,1"
                             HorizontalContentAlignment="Stretch"
                             BorderBrush="LightGray"
                             ItemsSource="{Binding Source={StaticResource ItemsSource}}"
                             ItemTemplateSelector="{StaticResource ShapeTemplateSelector}"
                             LostFocus="listBox_LostFocus">

                        <ListBox.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.ContainerStyle>
                                    <Style TargetType="{x:Type GroupItem}">
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                                    <Expander IsExpanded="True" Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type shapes:GenericUmlContainerShape}}, Path=GroupHeaderBackgroundBrush}">
                                                        <Expander.Header>
                                                            <TextBlock Text="{Binding Path=Name}"/>
                                                        </Expander.Header>
                                                        <Expander.Content>
                                                            <Grid Background="White">
                                                                <ItemsPresenter/>
                                                            </Grid>
                                                        </Expander.Content>
                                                    </Expander>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                </GroupStyle.ContainerStyle>
                            </GroupStyle>
                        </ListBox.GroupStyle>
                    </ListBox>

                </StackPanel>

            </Border>

            <!--</aero:SystemDropShadowChrome>-->

            <ControlTemplate.Triggers>

                <Trigger SourceName="_listBox" Property="HasItems" Value="False">
                    <Setter TargetName="_listBox" Property="Visibility" Value="Collapsed"/>
                </Trigger>

            </ControlTemplate.Triggers>

        </ControlTemplate>

    </Control.Template>

</Control>
